//
//  CYAuthManager.m
//  cyjf
//
//  Created by tauCross on 2017/5/4.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYAuthManager.h"

#import "CYSudokuManager.h"



@implementation CYAuth

MJExtensionCodingImplementation

@end



@interface CYAuthManager ()

@property(nonatomic, strong)NSString *path;

@end


@implementation CYAuthManager

+ (instancetype)standardManager
{
    static CYAuthManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CYAuthManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if(self)
    {
        self.path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"cyjf_current_auth"];
        CYAuth *auth = [NSKeyedUnarchiver unarchiveObjectWithFile:self.path];
        if(auth)
        {
            self.currentAuth = auth;
        }
    }
    return self;
}

- (void)saveAuth
{
    if(self.currentAuth)
    {
        [NSKeyedArchiver archiveRootObject:self.currentAuth toFile:self.path];
    }
    else
    {
        [CYSudokuManager removeSudokuPassword];
        [[NSFileManager defaultManager] removeItemAtPath:self.path error:nil];
    }
}

@end
